package streamforce.workload;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import streamforce.Stat;
import streamforce.crypto.event.StockEvent;
import streamforce.crypto.owner.EPOwnerClient;

/**
 * Gathering stats about the owner
 *
 */
public class OwnerWorkloadSingle {
	
	//<streamId> [-ne] <userId>  
	public static void main(String[] args) throws IOException{
		int streamId = new Integer(args[0]).intValue();
		int nTuples = new Integer(args[1]).intValue(); 	
		Properties prop = new Properties();
		int aggType = 0; 
		int userMaxId=0; 
		try {
			prop.load(new FileInputStream("attributes.properties"));
			userMaxId = new Integer(prop.getProperty("maxUserId")).intValue();
			aggType = new Integer(prop.getProperty("aggregateType")).intValue();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Timer t = new Timer("Owner task", true);    	
    	t.scheduleAtFixedRate(new TimerTask(){
    		public void run(){
    			Stat.aggregateAndResetAll("ownerThroughput");     			
    		}
    	}, 0, 1000);
    	
		
		EPOwnerClient owner = new EPOwnerClient(streamId); 
		
		if (args.length <= 1 || !args[1].equals("-ne")) {

			switch (aggType) {
			case -1:
				//EPOwnerClient.initSimpleClient(owner, streamId, 0);
				break;			
			case 1:
				//EPOwnerClient.initAggregateClient_1(owner, streamId, 0, 4);				
				break;
			case 2:
				//EPOwnerClient.initAggregateClient_2(owner, streamId, 0, 4);
				break;
			case 3:
				//EPOwnerClient.initAggregateClient_3(owner, streamId, 0, 4, 100000);
				break;
			}				
 
			int ts = 0; 
			int hour = 0; 
			Random rand = new Random(streamId); 
			do{				
				StockEvent sd = owner.encryptData(ts, hour, streamId, rand.nextInt(20), rand.nextInt(100));
				ts++;
				if (ts%60==0)
					hour++; 				
				//serialize to file
				//oos.writeObject(sd); 
			}while (ts <nTuples);
			//oos.writeObject(ses);
			//oos.close(); 
			EPOwnerClient.initSimpleClient(owner, streamId, 0);
			
		}
	}
}
